﻿//-------------------------------------------------------------------------------
// <copyright file="WebRequestParameter.cs" company="TaskyMedia LLC">
//     Copyright (c) TaskyMedia LLC.
// </copyright>
//-------------------------------------------------------------------------------

/*
 * Copyright (c) 2010, TaskyMedia LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *   Redistributions of source code must retain the above copyright notice, this 
 *     list of conditions and the following disclaimer.
 *   Redistributions in binary form must reproduce the above copyright notice, this
 *     list of conditions and the following disclaimer in the documentation and/or
 *     other materials provided with the distribution.
 *   Neither the name of TaskyMedia LLC nor the names of its contributors may be used
 *   to endorse or promote products derived from this software without specific prior
 *   written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE. 
 */

namespace FBGraphApi
{
    using System;
    using System.Globalization;
    using System.Text;
    using System.Web;

    /// <summary>
    /// A key/value pair used as a parameter in web requests.
    /// </summary>
    internal class WebRequestParameter
    {
        /// <summary>
        /// The key of the parameter.
        /// </summary>
        private string _key;

        /// <summary>
        /// The value of the parameter.
        /// </summary>
        private string _value;

        /// <summary>
        /// Initializes a new instance of the <see cref="WebRequestParameter"/> class with the specified key and an empty value.
        /// </summary>
        /// <param name="key">The key of the parameter.</param>
        public WebRequestParameter(string key)
            : this(key, String.Empty)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WebRequestParameter"/> class with the specified key and value.
        /// </summary>
        /// <param name="key">The key of the parameter.</param>
        /// <param name="value">The value of the parameter.</param>
        public WebRequestParameter(string key, string value)
        {
            this._key = key;
            this._value = value;
        }

        /// <summary>
        /// Gets or sets the key of the parameter.
        /// </summary>
        /// <value>The key of the parameter.</value>
        public string Key
        {
            get
            {
                return this._key;
            }

            set
            {
                this._key = value;
            }
        }

        /// <summary>
        /// Gets or sets the value of the parameter.
        /// </summary>
        /// <value>The value of the parameter.</value>
        public string Value
        {
            get
            {
                return this._value;
            }

            set
            {
                this._value = value;
            }
        }

        /// <summary>
        /// Builds a query string from the provided list of <see cref="WebRequestParameter" />s.
        /// </summary>
        /// <param name="webParameters">The parameters from which to build the query string.</param>
        /// <returns>A query string of all parameters, URL encoded.</returns>
        public static string BuildQueryString(WebRequestParameter[] webParameters)
        {
            StringBuilder sb = new StringBuilder();

            foreach (WebRequestParameter param in webParameters)
            {
                if (sb.Length > 0)
                {
                    sb.Append("&");
                }

                sb.Append(param.ToString());
            }

            return sb.ToString();
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return String.Format(CultureInfo.InvariantCulture, "{0}={1}", HttpUtility.UrlEncode(this._key, Encoding.UTF8), HttpUtility.UrlEncode(this._value, Encoding.UTF8));
        }
    }
}
